package googlemap;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Connor Madden.
 * User: Connor Madden
 * Date: 1/04/12
 * Time: 4:47 PM
 */
public class GoogleStaticMapFactory {

    public enum Format {
        PNG,
        PNG32,
        GIF,
        JPG,
        JPG_BASELINE
    }

    public enum MapType {
        ROAD_MAP,
        SATELLITE,
        TERRAIN,
        HYBRID
    }

    private static final String STATIC_MAP_URL =
            "http://maps.googleapis.com/maps/api/staticmap?";

    public static MapImageI newGoogleStaticMap(LatLng latLng, int zoom, int sizeWidth, int sizeHeight,
                                               int scale, Format format, MapType mapType) {

        LatLng verifiedLatLng = verifyLatLng(latLng);
        int verifiedZoom = verifyZoom(zoom);
        int verifiedSizeWidth = verifySize(sizeWidth);
        int verifiedSizeHeight = verifySize(sizeHeight);
        int verifiedScale = VerifyScale(scale);
        String verifiedFormat = VerifyFormat(format);
        String verifiedMapType = VerifyMapType(mapType);

        String urlString =
                STATIC_MAP_URL +
                        "center=" + verifiedLatLng.getLat() + "," + verifiedLatLng.getLng() +
                        "&" + "zoom=" + verifiedZoom +
                        "&" + "size=" + verifiedSizeWidth + "x" + verifiedSizeHeight +
                        "&sensor=false" +
                        "&scale=" + verifiedScale +
                        "&format=" + verifiedFormat +
                        "&maptype=" + verifiedMapType;
        URL url = null;
        try {
            url = new URL(urlString);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(url);
        } catch (IOException e) {
            System.out.println("LOG: Google Map ImageIO read failed");  // DEBUG print LOG
            System.out.println(url);
        }
        if (bufferedImage == null) {
            bufferedImage = new BufferedImage(verifiedSizeWidth, verifiedSizeHeight, 1); // Create blank RGB image
        }
        return new GoogleStaticMap(bufferedImage, verifiedZoom, verifiedLatLng);
    }

    private static LatLng verifyLatLng(LatLng latLng) {
        return GoogleStaticMap.validateLatLng(latLng);
    }

    private static int verifyZoom(int zoom) {
        if (zoom > 21) {
            zoom = 21;
        } else if (zoom < 0) {
            zoom = 0;
        }
        return zoom;
    }

    private static int verifySize(int n) {
        if (n > 640) {
            n = 640;
        } else if (n <= 0) {
            n = 1;
        }
        return n;
    }

    private static int VerifyScale(int scale) {
        if (scale > 2) {
            scale = 2;
        } else if (scale <= 0) {
            scale = 1;
        }
        return scale;
    }

    private static String VerifyFormat(Format format) {
        switch (format) {
            case PNG:
                return "png";
            case PNG32:
                return "png32";
            case GIF:
                return "gif";
            case JPG:
                return "jpg";
            case JPG_BASELINE:
                return "jpg-baseline";
            default:
                return "";
        }
    }

    private static String VerifyMapType(MapType mapType) {
        switch (mapType) {
            case ROAD_MAP:
                return "roadmap";
            case SATELLITE:
                return "satellite";
            case TERRAIN:
                return "terrain";
            case HYBRID:
                return "hybrid";
            default:
                return "";
        }
    }
}
